package net.orange.architecture.web;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;

/**
 * Localization message provider, it can provide the message according the
 * language of user IE.
 */
public class LocalizationMessage {
	private Properties content = new Properties();

	private static HashMap<String, LocalizationMessage> instance = new HashMap<String, LocalizationMessage>();

	/**
	 * Disable default contrustor.
	 */
	private LocalizationMessage() {
		return;
	}

	/**
	 * Constuctor with the resource file url.
	 * 
	 * @param url
	 *            the resource file url
	 */
	private LocalizationMessage(String url) throws Exception {
		InputStream is = LocalizationMessage.class.getClassLoader()
				.getResourceAsStream(url);
		content.load(is);
		is.close();
	}

	/**
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static LocalizationMessage getInstance(String url) throws Exception {
		String filename = url + ".properties";
//		System.out.print("Local message file-->" + filename);
		LocalizationMessage message = instance.get(filename);
		if (message != null)
			return message;

		message = new LocalizationMessage(filename);
		instance.put(filename, message);
		return message;
	}

	/**
	 * Return the localization message with the index.
	 * 
	 * @param index
	 *            the message index
	 * @return the localization message with the index.
	 */
	public String get(String index) {
		String message = (String) content.get(index);
		return (message != null && message.length() > 0) ? message : index;
	}
}
